package com.example.usingjsontocallwcf;

import java.io.IOException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.example.Wcf.ConnectWcf;

import android.content.Context;
import android.os.AsyncTask;
import android.widget.TextView;
import android.widget.Toast;

public class wcfAsyntask extends AsyncTask<Void, Void, String>{

	Context context;
	TextView textView;
	private JSONObject jsonObject;
	HttpClient httpClient;
	public wcfAsyntask(Context context,TextView textView,HttpClient httpClient)
	{
		this.httpClient=httpClient;
		this.context=context;
		this.textView=textView;
	}
	@Override
	protected String doInBackground(Void... params) {
		

			String base="http://10.0.2.2:8011/vanhung/VanhungJson";
			Sinhvien sinhvien=new Sinhvien("name", "07/14/2013", "dsds");
			LoaiHang loaiHang=new LoaiHang("nguyen van hug");
			
			ConnectWcf conect=new ConnectWcf();
			try {
			
			return conect.Connect_Wcf(base,"vanhung",loaiHang,httpClient);
				
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
         return null;
	}

	@Override
	protected void onPostExecute(String result) {
		// TODO Auto-generated method stub
		
		try {
			JSONArray jsonArray=new JSONArray(result);
			for (int i = 0; i < jsonArray.length(); i++) {
				jsonObject=jsonArray.getJSONObject(i);
				Show(jsonObject.getString("Ten"));
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	protected void onPreExecute() {
		// TODO Auto-generated method stub
		super.onPreExecute();
	}
	
	//method manuall
	
	private void Show(String resulString) {
		// TODO Auto-generated method stub
		Toast.makeText(context, resulString	, Toast.LENGTH_SHORT).show();
	}
}
